package guowenlong.golf.ui.personal.changephone.step_three;


import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.Toast;
import butterknife.BindView;
import butterknife.OnClick;
import guowenlong.golf.C;
import guowenlong.golf.R;
import guowenlong.golf.base.BaseFragment;
import guowenlong.golf.entity.RegistEntity;
import guowenlong.golf.widget.IOSEditText;
import me.wenlong.mixutils.MixUtils;

public class StepThreeFragment extends BaseFragment<StepThreePresenter, StepThreeModel> implements
    StepThreeContract.View {

  @BindView(R.id.et_password)
  IOSEditText etPassword;
  @BindView(R.id.et_repassword)
  IOSEditText etRepassword;
  @BindView(R.id.btn_regist)
  Button btnRegist;

  @Override
  public int getLayoutId() {
    return R.layout.fragment_step_three;
  }

  @Override
  public void initView() {
    etPassword.setTextChangedAfterListener(new IOSEditText.TextChangedAfterListener() {
      @Override
      public void onTextChangedAfter(Editable s) {
        if (s.toString().length() < 6) {
          btnRegist.setBackgroundResource(R.drawable.bg_green_regist_normal);
          btnRegist.setClickable(false);
        }
      }
    });
    etRepassword.setTextChangedAfterListener(new IOSEditText.TextChangedAfterListener() {
      @Override
      public void onTextChangedAfter(Editable s) {
        if (s.toString().length() >= 6 && TextUtils
            .equals(s.toString(), etPassword.getText().toString())) {
          btnRegist.setBackgroundResource(R.drawable.bg_green_regist_pressed);
          btnRegist.setClickable(true);
        } else {
          btnRegist.setBackgroundResource(R.drawable.bg_green_regist_normal);
          btnRegist.setClickable(false);
        }
      }
    });
    etPassword.requestFocus();
  }

  @Override
  public void initData(Bundle savedInstanceState) {

  }

  @OnClick(R.id.btn_regist)
  public void onClick() {
    String phone_number = MixUtils.mSp.getString(C.PHONE_NUMBER, "");
    String code = MixUtils.mSp.getString(C.CODE, "");
    mPresenter.create(phone_number, code, etPassword.getText().toString().trim());
  }

  @Override
  public void createSuccess(RegistEntity registEntity) {
    Toast.makeText(getActivity(), registEntity.getMessage(), Toast.LENGTH_SHORT).show();
    MixUtils.mSp.edit().putString(C.PHONE_NUMBER, "").commit();
    MixUtils.mSp.edit().putString(C.CODE, "").commit();
    getActivity().setResult(Activity.RESULT_OK);
    getActivity().finish();
  }

  @Override
  public void createError(RegistEntity registEntity) {
    Toast.makeText(getActivity(), registEntity.getError_message(), Toast.LENGTH_SHORT).show();
  }
}
